using System;
using System.Collections.Generic;
using System.Text;
using PDSClient.Simple;
using PDSClient;
using PDSClient.Utils;
using PDSClient.Utils.Shared;


namespace HelloUserClient
{
    class Program : ISimpleClientListener, IClientChannelListener
    {
        private const string host = "localhost";
        private const string port = "1139";
        private SimpleClient client;

        private List<IClientChannel> channels = new List<IClientChannel>();

        public Program()
        {
            //AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
            client = new SimpleClient(this);
        }

        void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine((e.ExceptionObject as Exception).Message);
            Console.WriteLine("======================================");
            Console.WriteLine((e.ExceptionObject as Exception).StackTrace);
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Login();
            string input = "";
            while ((input = Console.ReadLine()) != "x")
                p.SendMessage(input);
        }

        private void SendMessage(string input)
        {
            client.Send(System.Text.UTF8Encoding.UTF8.GetBytes(input));
            // channels[0].Send(System.Text.UTF8Encoding.UTF8.GetBytes(input));
        }
        public void Login()
        {
            IDictionary<string, string> prop = new Dictionary<string, string>();
            prop.Add("host", host);
            prop.Add("port", port);

            client.Login(prop);
        }

        public void LoggedIn()
        {
            Console.WriteLine("logged in");
        }

        public void LoginFailed(string reason)
        {
            Console.WriteLine("login failed");
        }

        public IClientChannelListener JoinedChannel(IClientChannel channel)
        {
            channels.Add(channel);
            Console.WriteLine("joined channel");
            return this;
        }

        public void ReceivedMessage(byte[] message)
        {
            Console.WriteLine(System.Text.UTF8Encoding.UTF8.GetString(message));
        }

        public void Reconnecting()
        {
            Console.WriteLine("reconnecting");
        }

        public void Reconnected()
        {
            Console.WriteLine("reconnected");
        }

        public void Disconnected(bool graceful, string reason)
        {
            Console.WriteLine("disconnected  " + reason);
        }

        public PasswordAuthentication PasswordAuthentication
        {
            get
            {
                return new PasswordAuthentication("helloUserUser", "password".ToCharArray());
            }
        }



        #region IClientChannelListener Members

        public void ReceivedMessage(IClientChannel channel, SessionId sender, byte[] message)
        {
            Console.WriteLine(channel.Name + ": " + System.Text.UTF8Encoding.UTF8.GetString(message));
        }

        public void LeftChannel(IClientChannel channel)
        {
            channels.Remove(channel);
            Console.WriteLine("left channel");
        }

        #endregion
    }
}
